#!/bin/bash

[[ -n $DEBUG ]] && set -x
set -euo pipefail
IFS=$'\n\t'
version="202510100041"
prog=$(basename "$0")
dfhostm_path=$(realpath "$0")
dfhostm_dirhost_dir=$(dirname "${dfhostm_path}")
dfhostm_config_path="${HOME}/.config/dotfiles-configs/dfhostm.jsonc"
default_editor=$(git config --global --get core.editor || echo "${EDITOR:-vi}")

rc_path="${HOME}/.bashrc"

declare -gA CFG # 全局数组
CFG="CFG"

###################################################
# log function
###################################################

_info() {
  echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $*"
}

_warn() {
  echo -e "\033[1;33m[$(date +'%Y-%m-%d %H:%M:%S')]: $*\033[0m" >&2
}
_err() {
  echo -e "\033[1;31m[$(date +'%Y-%m-%d %H:%M:%S')]: $*\033[0m" >&2
  exit 1
}

usage() {
  echo "================================================================================"
  echo "Descp: $prog manages host"
  echo "Version: ${version}"
  echo "Editor: ${default_editor}"
  echo "Usage: $prog"
  config_usage
  echo ""
  refresh_usage
  echo ""
  job_usage
  echo ""
  echo "================================================================================"
}

get_default_config() {
  cat <<EOF
{
  // specific dns
  "dns": "8.8.8.8",
  // target hosts which will be transforming
  "target_hosts": [
    "github.githubassets.com",
    "central.github.com",
    "desktop.githubusercontent.com",
    "assets-cdn.github.com",
    "camo.githubusercontent.com",
    "github.map.fastly.net",
    "github.global.ssl.fastly.net",
    "gist.github.com",
    "github.io",
    "github.com",
    "api.github.com",
    "raw.githubusercontent.com",
    "user-images.githubusercontent.com",
    "favicons.githubusercontent.com",
    "avatars5.githubusercontent.com",
    "avatars4.githubusercontent.com",
    "avatars3.githubusercontent.com",
    "avatars2.githubusercontent.com",
    "avatars1.githubusercontent.com",
    "avatars0.githubusercontent.com",
    "avatars.githubusercontent.com",
    "codeload.github.com",
    "github-cloud.s3.amazonaws.com",
    "github-com.s3.amazonaws.com",
    "github-production-release-asset-2e65be.s3.amazonaws.com",
    "github-production-user-asset-6210df.s3.amazonaws.com",
    "github-production-repository-file-5c1aeb.s3.amazonaws.com",
    "githubstatus.com",
    "github.community",
    "media.githubusercontent.com"
  ]
}
EOF
}

load_configs() {
  check_config_if_exist
  local cfg=""

  cfg=$(cat "${dfhostm_config_path}")

  eval "$(
    jq -r '
      @sh "cfg_dns=\(.dns)",
      @sh "cfg_target_hosts=\(.target_hosts)"
    ' <<<cfg
  )"
}

list_configs() {
  check_config_if_exist
  cat "${dfhostm_config_path}"
}

check_config_if_exist() {
  if [[ ! -f "${dfhostm_config_path}" ]]; then
    _err "${dfhostm_config_path} does not exist, please init by '$prog' config init"
  fi
}

config_usage() {
  echo "Kind:  config"
  echo "  e.g. $prog config                              note: show config subcommand usage"
  echo "       $prog config init                         note: init config"
  echo "       $prog config list                         note: list config"
  echo "       $prog config edit                         note: edit config, init firstly if config does not exist"

}

execute_subcmd_config() {
  local cmd=${1:-""}

  case "${cmd}" in
  init)
    if [ -f "${dfhostm_config_path}" ]; then
      _info "${dfhostm_config_path} has existed, skip init"
      exit 0
    fi
    mkdir -p "$(dirname ${dfhostm_config_path})"
    get_default_config >"${dfhostm_config_path}"
    ;;
  list)
    load_configs
    list_configs
    ;;
  edit)
    if [ ! -f "${dfhostm_config_path}" ]; then
      _info "proxy config: ${dfhostm_config_path} has not existed, init now..."
      mkdir -p "$(dirname ${dfhostm_config_path})"
      get_default_config >"${dfhostm_config_path}"
    fi
    "${default_editor}" "${dfhostm_config_path}"
    ;;
  *)
    config_usage
    _err "unknown subcmd: ${cmd}"
    ;;
  esac

}

refresh_usage() {
  echo "Kind:  refresh"
  echo "  e.g. $prog refresh                             note: show refresh subcommand usage"
  echo "       $prog refresh dns [-v]                    note: refresh dns info to host"
}

execute_subcmd_refresh() {
  exit 0
}

job_usage() {
  echo "Kind:  job"
  echo "  e.g. $prog job                                 note: show job subcommand usage"
  echo "       $prog job enable                          note: show status of docker proxy"
}

execute_subcmd_job() {
  exit 0
}

main() {
  if [[ $# -lt 1 || "$1" == "-h" || "$1" == "--help" ]]; then
    # show help
    usage
    exit 0
  fi
  case "${1}" in
  "config") execute_subcmd_config "${@:2}" ;;
  "refresh") execute_subcmd_refresh "${@:2}" ;;
  "job") execute_subcmd_job "${@:2}" ;;
  *) usage ;;
  esac

}

main "$@"
